package com.ytc.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ytc.commons.model.vo.ResponseResult;
import com.ytc.commons.model.vo.TeacherDto;
import com.ytc.content.domain.CourseTeacher;
import com.ytc.content.mappers.CourseTeacherMapper;
import com.ytc.content.service.CourseTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ytc.web.interceptor.AuthInfoHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-01
 */
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {


    //设置讲师
    @Override
    public ResponseResult<List<CourseTeacher>> listTeacher(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> qw = new LambdaQueryWrapper<>();
        qw.eq(CourseTeacher::getCourseId,courseId);
        List<CourseTeacher> list = this.list(qw);

        return ResponseResult.okResult(list);
    }

    //讲师新增
    @Override
    public ResponseResult saveTeacher(TeacherDto dto) {
        CourseTeacher one = this.getById(dto.getId());


        if(one==null){
            CourseTeacher courseTeacher = new CourseTeacher();
            courseTeacher.setCompanyId(AuthInfoHolder.getCompanyId());
            courseTeacher.setCourseId(dto.getCourseId());
            courseTeacher.setCreateDate(new Date());
            courseTeacher.setPhotograph(dto.getPhotograph());
            courseTeacher.setIntroduction(dto.getIntroduction());
            courseTeacher.setPosition(dto.getPosition());
            courseTeacher.setTeacherName(dto.getTeacherName());
            this.save(courseTeacher);
        }else {
            CourseTeacher courseTeacher = new CourseTeacher();
            courseTeacher.setId(dto.getId());
            courseTeacher.setPhotograph(dto.getPhotograph());
            courseTeacher.setIntroduction(dto.getIntroduction());
            courseTeacher.setPosition(dto.getPosition());
            courseTeacher.setTeacherName(dto.getTeacherName());
            this.updateById(courseTeacher);
        }


        return ResponseResult.okResult(true);
    }
}
